package com.rockxin.cat.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.base.Constant;
import com.rockxin.cat.base.User;
import com.rockxin.cat.bean.RedPacketBean;
import com.rockxin.cat.network.CatNetWork;
import com.rockxin.cat.network.HttpResult;
import com.rockxin.cat.utils.SPUtils;
import com.rockxin.cat.utils.ToastUtils;

import java.util.List;

import butterknife.BindView;
import rx.Subscriber;

/**
 * Created by cat on 18-3-4.
 */

public class BidActivity extends BaseActivity {

    @BindView(R.id.back_img)
    ImageView backImg;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_release_date)
    TextView tvReleaseDate;
    @BindView(R.id.tv_rate_num)
    TextView tvRateNum;
    @BindView(R.id.tv_rate)
    TextView tvRate;
    @BindView(R.id.tv_term)
    TextView tvTerm;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.et_bid_money)
    EditText etBidMoney;
    @BindView(R.id.tv_profit_num)
    TextView tvProfitNum;
    @BindView(R.id.tv_balance_num)
    TextView tvBalanceNum;
    @BindView(R.id.tv_coupon)
    TextView tvCoupon;
    @BindView(R.id.tv_risk_hints)
    TextView tvRiskHints;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    private String targetId;

    private String mCoupon = "0";

    @Override
    protected int setLayout() {
        return R.layout.activity_bid;
    }

    @Override
    public void initData() {
        initView();
        setListener();
        getData();
    }

    public void initView() {
        title.setText("投标");
        Intent getIntent = getIntent();
        targetId = getIntent.getStringExtra("targetId");

        etBidMoney.addTextChangedListener(textWatcher);

        tvBalanceNum.setText(User.getInstance().getUserInfo().getAll_money() + "");

        SPUtils spUtils = new SPUtils("Guide");

        tvRateNum.setText(spUtils.getString(Constant.PREFERENCES.RATENUM));
        tvRate.setText(spUtils.getString(Constant.PREFERENCES.RATE));
        tvTerm.setText(spUtils.getString(Constant.PREFERENCES.TERM));
        tvMoney.setText(spUtils.getString(Constant.PREFERENCES.BORROWMONEY));
    }

    private void setListener() {
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvRiskHints.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BidActivity.this, HtmlLocalActivity.class);
                intent.putExtra("type", "riskHints");
                startActivity(intent);
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                submit();
            }
        });
    }

    public void getData() {
        getVoucher();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String value = s.toString();
            if (value.length() == 1 && value.equals("0")) {
                etBidMoney.setText("");
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
//            Editable ea = et_pay_money.getText();
//            int count = ea.length();
//            Selection.setSelection(ea, count == 0 ? 0 : count);
        }
    };


    private void getVoucher() {
        SPUtils spUtils = new SPUtils("Guide");
        String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
        CatNetWork catNetWork = new CatNetWork();
        catNetWork.myVoucher(1, 1, token, 1, new Subscriber<HttpResult<List<RedPacketBean>>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult<List<RedPacketBean>> listHttpResult) {
                if (listHttpResult.getStatus() == 1) {
                    RedPacketBean data = listHttpResult.getData().get(0);
                    tvCoupon.setText(data.getAdd_rate_format());
                    mCoupon = data.getId();
                }
            }
        });
    }

    private void submit() {
        String money = etBidMoney.getText().toString();
        if (money.length() == 0) {
            ToastUtils.showShortToastSafe("金额不能小于100元");
            return;
        } else if (Integer.parseInt(money) < 100) {
            ToastUtils.showShortToastSafe("金额不能小于100元");
            return;
        }

        SPUtils spUtils = new SPUtils("Guide");
        String token = spUtils.getString(Constant.PREFERENCES.TOKEN);
        final CatNetWork catNetWork = new CatNetWork();
        catNetWork.bid(token, targetId, money, mCoupon, new Subscriber<HttpResult>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpResult httpResult) {
                Logger.e("CatNetWork------->" + httpResult.toString());
                if (httpResult.getStatus() == 1) {
                    Intent intent = new Intent(BidActivity.this, BidSuccessActivity.class);
                    startActivity(intent);
                }
            }
        });

    }

}
